/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-05-18 09:59:00
 * @LastEditTime : 2023-06-27 13:55:46
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Router\MenuNav\module\TableList.jsx
 * @Description  :
 */
import { useEventEmitter } from 'ahooks'
import request from 'umi-request'
import { useState, useEffect, useRef } from 'react'
import { ProTable } from '@ant-design/pro-components'
import { ModalBox } from '../../../Modal/ModalBox'

export const TableList = ({ height, TableListData, router }) => {
    const actionRef = useRef()
    const headers = { Authorization: sessionStorage.getItem('TOKEN') }
    const [paramsData, setParamsData] = useState(1)
    const [SizeNumber, setSizeNumber] = useState(10)
    const [TableListParament, setTableListParament] = useState()
    const [OptionsCall, setOptionsCall] = useState()
    const OptionsEvent = useEventEmitter()
    OptionsEvent.useSubscription(val => setOptionsCall(val))

    useEffect(() => {
        const { page, ...res } = TableListData
        const ClientData = JSON.parse(localStorage.getItem('ClientData'))
        const getProList = async () => {
            const Template = await import(`../${router}/${page}`).catch(() => false)
            setTableListParament(await Template[page]({ ...res, OptionsEvent, ClientData, actionRef }))
        }
        if (page) getProList()
    }, [TableListData, OptionsEvent, router])

    return (
        <>
            {TableListParament && (
                <ProTable
                    actionRef={actionRef}
                    columns={TableListParament.columns}
                    params={TableListParament.params}
                    request={async (params = {}) => {
                        delete params.current
                        params.pageIndex = paramsData || 1
                        return request(TableListParament.get, { params, headers }).then(data => {
                            if (data.list) return { data: data.list, total: data.total, success: true }
                            else return { data, success: true }
                        })
                    }}
                    pagination={{
                        pageSize: SizeNumber,
                        showSizeChanger: true,
                        onChange: (e, a) => {
                            setParamsData(e)
                            setSizeNumber(a)
                        },
                    }}
                    columnsState={{ persistenceKey: 'pro-table-singe-demos', persistenceType: 'localStorage' }}
                    rowKey="uuid"
                    toolBarRender={() => TableListParament.options}
                    search={TableListParament.hideInSearch ? false : true}
                    scroll={{
                        y:
                            router === 'TwoRouter'
                                ? TableListParament.hideInSearch
                                    ? height - 168
                                    : height - 260
                                : height - 260,
                    }}
                />
            )}
            <ModalBox OptionsCall={OptionsCall} actionRef={actionRef} configure={TableListParament} />
        </>
    )
}
